package com.project.controller.web;

import com.project.common.utils.FileUtil;
import com.project.common.utils.Util;
import com.project.myBeans.R;
import org.apache.commons.io.FileUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

/**
 * @Author: TateBrown
 * @date: 2018/5/26 16:40
 * @param:
 * @return:
 */
@EnableAutoConfiguration
@RestController
@RequestMapping("/download")
@Api(description = "下载相关接口", value = "download")
public class DownloadController {
    /**
     * TateBrown 2018年5月26日
     *
     * @param name
     * @return
     */
    @ApiOperation("下载导入失败数据excel表")
    @PostMapping("/errorData/{name}")
    public ResponseEntity<byte[]> errorData(@PathVariable("name") String name, HttpServletRequest request){
        //文件路径待定
        String savaPath=request.getSession().getServletContext().getRealPath("/dist/static/temp");
        String filename=savaPath+"/"+name+".xlsx";
        File file=new File(filename);
        try{
            byte[] data= FileUtils.readFileToByteArray(file);
            FileUtil.delFile(filename);
            HttpHeaders headers=new HttpHeaders();
            filename=new String("导入失败数据.xlsx".getBytes("UTF-8"), "iso-8859-1");
            headers.setContentDispositionFormData("attachment", filename);
            //设置MIME类型 以下载方式
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            HttpStatus statusCode = HttpStatus.OK;
            return new ResponseEntity<byte[]>(data, headers, statusCode);
        }catch(Exception e){
            e.printStackTrace();
            return null;
        }
    }

    /**
     * TateBrown 2018年5月26日
     *
     * @param request
     * @return
     */
    @ApiOperation("下载学生数据上传模板")
    @PostMapping("/StudentTemp")
    public ResponseEntity<byte[]> studentTemp(HttpServletRequest request){
        //绝对路径
        String rootPath = Util.getWebRootURL(this);

//        String savePath = request.getSession().getServletContext().getRealPath("/WEB-INF/static");
        String name = "studentTemplate.xlsx";
        String url = rootPath + "/" + "template" + "/" + name ;
        File file = new File(url);
        try {
            byte[] data = FileUtils.readFileToByteArray(file);
            HttpHeaders headers = new HttpHeaders();
            String filename = new String("学生数据上传模板".getBytes("UTF-8"), "iso-8859-1");
            headers.setContentDispositionFormData("attachment", filename);
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);//设置MIME类型 以下载方式
            HttpStatus statusCode = HttpStatus.OK;
            return new ResponseEntity<byte[]>(data, headers, statusCode);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
    /**
     * TateBrown 2018年5月26日
     *
     * @param request
     * @return
     */
    @ApiOperation("下载教师数据上传模板")
    @PostMapping("/TeacherTemp")
    public ResponseEntity<byte[]> teacherTemp(HttpServletRequest request){
        //绝对路径
        String rootPath = Util.getWebRootURL(this);

//        String savePath = request.getSession().getServletContext().getRealPath("/WEB-INF/static");
        String name = "teacherTemplate.xlsx";
        String url = rootPath + "/" + "template" + "/" + name ;
        File file = new File(url);
        try {
            byte[] data = FileUtils.readFileToByteArray(file);
            HttpHeaders headers = new HttpHeaders();
            String filename = new String("教师数据上传模板".getBytes("UTF-8"), "iso-8859-1");
            headers.setContentDispositionFormData("attachment", filename);
            //设置MIME类型 以下载方式
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            HttpStatus statusCode = HttpStatus.OK;
            return new ResponseEntity<byte[]>(data, headers, statusCode);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}
